package com.glq1218.article.controller;

import com.glq1218.article.bo.AddArticleBO;
import com.glq1218.article.bo.ArticleBO;
import com.glq1218.article.bo.QueryArticleBO;
import com.glq1218.article.bo.UpdateArticleBO;
import com.glq1218.article.dto.AddArticleDTO;
import com.glq1218.article.dto.ArticleDTO;
import com.glq1218.article.dto.QueryArticleDTO;
import com.glq1218.article.dto.UpdateArticleDTO;
import com.glq1218.article.service.ArticleService;
import com.glq1218.domain.*;
import com.glq1218.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author glq1218
 * @description 物品控制器
 * @since 2023/4/21 14:45
 */
@RestController
@RequestMapping("/api/article")
@RequiredArgsConstructor
public class ArticleController {
    private final ArticleService articleService;

    @GetMapping
    public R<PaginationResult<ArticleDTO>> queryArticleList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryArticleDTO queryArticleDTO) {
        QueryArticleBO queryArticleBO = BeanCopyUtils.copyBean(queryArticleDTO, QueryArticleBO.class);
        // 调用service获取用户信息
        PaginationResult<ArticleBO> paginationResult = articleService.queryArticleList(paginationParam, sortParam, intervalStr, queryArticleBO);
        // 获取BO转换为DTO
        List<ArticleDTO> articleDTOList = new ArrayList<>();
        // 结果不为空才能进行转换
        if (Objects.nonNull(paginationResult.getRecords())) {
            List<ArticleBO> records = paginationResult.getRecords();
            articleDTOList = BeanCopyUtils.copyBeanList(records, ArticleDTO.class);
        }
        // 返回封装好的分页查询DTO
        return R.ok(PaginationResult.<ArticleDTO>builder()
                .total(paginationResult.getTotal())
                .records(articleDTOList)
                .build()
        );
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<ArticleDTO> getArticleById(@PathVariable Serializable id) {
        ArticleBO articleBO = articleService.getArticleById(id);
        if (articleBO == null) {
            return R.fail("不存在该id的用户信息");
        }
        ArticleDTO articleDTO = BeanCopyUtils.copyBean(articleBO, ArticleDTO.class);
        return R.ok(articleDTO);
    }

    @PostMapping
    public R<?> addArticle(@RequestBody AddArticleDTO addArticleDTO) {
        AddArticleBO addArticleBO = BeanCopyUtils.copyBean(addArticleDTO, AddArticleBO.class);
        return R.ok(articleService.addArticle(addArticleBO));
    }

    @PutMapping
    public R<?> updateArticle(@RequestBody UpdateArticleDTO updateArticleDTO) {
        UpdateArticleBO updateArticleBO = BeanCopyUtils.copyBean(updateArticleDTO, UpdateArticleBO.class);
        return R.ok(articleService.updateArticle(updateArticleBO));
    }

    @DeleteMapping
    public R<?> deleteArticle(@RequestParam("idList") List<Long> idList) {
        return R.ok(articleService.removeByIds(idList));
    }
}
